package com.fruits.login.service.impl;

import com.fruits.login.dao.HallUrlDAO;
import com.fruits.login.model.IPWhiteList;
import com.fruits.login.model.SmsCode;
import com.fruits.login.model.SmsCodeSingle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 定时器
 */
@Component
public class Jobs {
    private static final Logger logger = LoggerFactory.getLogger(Jobs.class);

    public final static long JOB_TIME =  60 * 1000 * 2;

    private SmsCodeSingle smsCodeSingle = new SmsCodeSingle();

    @Autowired
    private HallUrlDAO hallUrlDAO;

    @Scheduled(fixedDelay=JOB_TIME)
//    @Scheduled(cron="0 15 3 * * ?")
    public void cronJob(){
        refreshHallIp();
        clearPhoneCode();
    }

    /**
     * 定时刷新大厅白名单
     */
    private void refreshHallIp(){
        logger.info("大厅白名单列表:");
        List<String> ips = hallUrlDAO.findAllHallIp();
        IPWhiteList.setIps(ips);
        ips.forEach(ip ->logger.info(ip));
    }

    /**
     * 定时清理短信验证码
     */
    private void clearPhoneCode(){
//        logger.info("短信验证码清理之前");
//        SmsCodeSingle.instance().forEach(smsCode -> logger.info(smsCode.getPhone() + ":" + smsCode.getCode() + ":" +smsCode.getGenerateTime()));
        smsCodeSingle.scheduledCleaning();
//        logger.info("短信验证码清理之后");
//        SmsCodeSingle.instance().forEach(smsCode -> logger.info(smsCode.getPhone() + ":" + smsCode.getCode() + ":" +smsCode.getGenerateTime()));
    }
}
